<template>
	<view>
		<view v-for="(item,index) in devicelist" :key="index">
			<view class="uni-card">
				<view class="uni-card-header">
					<button type="warn" style="width: 140upx;margin: 2upx;" @tap="deletone" :data-stepid="index">删除</button>
					{{item.name}}
					<view class="zaixian" v-if="item.isonline === 1">
						在线
					</view>
					<view class="nozaixian" v-if="item.isonline === 0">
						离线
					</view>
				</view>
				<view class="uni-card-content">
					<view class="uni-card-content-inner">
						<view class="uni-flex uni-row">
							经纬度: &nbsp;{{item.lat}},&nbsp;{{item.lng}}
						</view>
					</view>
				</view>
				<view class="uni-card-footer">最后更新时间:&nbsp;{{item.update_time}}</view>
			</view>
		</view>
		<view class="uni-flex uni-row bottomview">
			<button type="primary" style="flex: 1;" @tap="addone">添加设备</button>
			<button type="primary" style="flex: 1;" @tap="onmap">查看地图</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null,
				num10: -1,
				username: '',
				password: '',
				version: 21,
				mobile: '',
				devicelist: [],
			};
		},
		onLoad(e) {
			// #ifdef APP-PLUS
			this.AndroidCheckUpdate();
			// #endif

			//5 分钟刷一次
			let _this = this;
			this.timer = setInterval(() => {
				if (_this.num10 > 0) {
					_this.num10++;
					_this.updateuserdevice();
				}
			}, 30000)
		},
		onShow(e) {
			uni.getStorage({
				key: 'username',
				success: (res) => {
					this.username = res.data;
				},
				fail: () => {

				}
			});
			uni.getStorage({
				key: 'password',
				success: (res) => {
					this.password = res.data;
				},
				fail: () => {
					this.password = "";
				}
			});

			if (this.password.length < 1 || this.username.length < 1) {
				uni.navigateTo({
					url: '../login/login',
				});
			} else {
				this.updateuserdevice();
			}
		},
		onNavigationBarButtonTap(e) {
			this.num10 = -1;
			uni.navigateTo({
				url: '../about/about?version=' + this.version,
			});
		},
		beforeDestroy() {
			clearInterval(this.timer)
		},
		methods: {
			AndroidCheckUpdate: function() {
				var _this = this;
				uni.request({
					//版本自动升级也使用内网ip
					url: this.url + '/fgversion.txt',
					method: 'GET',
					data: {},
					success: res => {
						if (res.data > this.version) {
							if (plus.networkinfo.getCurrentType() != 3) {
								uni.showToast({
									title: '有新的版本发布，检测到您目前非Wifi连接，为节约您的流量，程序已停止自动更新，将在您连接WIFI之后重新检测更新。',
									mask: false,
									duration: 5000,
									icon: "none"
								});
								return;
							}
							uni.showToast({
								title: '有新的版本发布，检测到您目前为Wifi连接，程序已启动自动更新。新版本下载完成后将自动弹出安装程序。',
								mask: false,
								duration: 5000,
								icon: "none"
							});
							var dtask = plus.downloader.createDownload(this.url + "/fgapp.apk", {}, function(d, status) {
								// 下载完成  
								if (status == 200) {
									plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename), {}, {}, function(error) {
										uni.showToast({
											title: '安装失败',
											mask: false,
											duration: 1500
										});
									})
								} else {
									uni.showToast({
										title: '更新失败',
										mask: false,
										duration: 1500
									});
								}
							});
							dtask.start();
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			onmap: function() {
				if (this.devicelist.length < 1) {
					uni.showToast({
						title: '请添加设备！',
						mask: false,
						duration: 1500
					});
					return;
				}
				uni.navigateTo({
					url: '../monitormap/monitormap'
				});
			},
			updateuserdevice: function(e) {
				let _this = this;
				uni.request({
					url: this.url + '/api/feige/getuserdevice?',
					data: {
						"username": this.username,
						"password": this.password
					},
					success: function(res) {
						_this.devicelist = res.data.data;
						_this.num10 = 1;
						// console.log(JSON.stringify(_this.devicelist));
					}
				});

			},
			addone() {
				if (this.devicelist.length >= 3) {
					uni.showToast({
						title: '最多添加3个设备！',
						mask: false,
						duration: 1500
					});
					return;
				}
				this.num10 = -1;
				uni.navigateTo({
					url: '../searchdevice/searchdevice',
				});
			},
			deletone: function(e) {
				let index = e.currentTarget.dataset.stepid;
				let _this = this;
				console.log(index + " ----");
				console.log(this.devicelist[index].name);
				uni.request({
					url: this.url + '/api/feige/deldevice?',
					data: {
						"username": this.username,
						"password": this.password,
						"mobile": this.devicelist[index].name,
					},
					success: function(res) {
						uni.showToast({
							title: '删除成功！',
							icon: 'none',
							mask: true,
							duration: 3000
						});
						console.log(index + " ----A");
						_this.devicelist.splice(index, 1);
					},
					fail: (err) => {
						uni.showToast({
							title: err,
							icon: 'none',
							mask: true,
							duration: 3000
						});
					},
				});
			}
		}
	}
</script>

<style>
	.title {
		font-size: 36upx;
		width: 265upx;
	}

	.zaixian {
		color: #00FF69;
		font-size: 36upx;
	}

	.nozaixian {
		color: #FF3333;
		font-size: 36upx;
	}

	.bottomview {
		position: absolute;
		width: 100%;
		left: 0upx;
		bottom: 0upx;
	}
</style>
